package com.xinke.juc.case04;

import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @Author：xinke
 * @Date：2025/10/1
 * @Desc: synchronized
 */
@Slf4j
public class SharedMemory02 {
    private static int counter = 0;
    private static final Object room = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (room) {
                    counter++;
                }

            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (room) {
                    counter--;
                }
            }
        }, "t2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        log.info("结果是：{}", counter);
    }
}
